package com.qeegoo.b2bautozityp.moudle.good.view;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.ActivityGoodsBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerGoodsActivityComponent;
import com.qeegoo.b2bautozityp.moudle.base.AppBar;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.base.FragmentPagerAdapter;
import com.qeegoo.b2bautozityp.moudle.good.viewmodel.GoodsViewModel;
import com.qeegoo.b2bautozityp.rxbus.event.GoodsEvent;
import com.qeegoo.b2bautozityp.utils.NavBarUtils;

import java.util.ArrayList;

import javax.inject.Inject;

/**
 * 王少岩 在 2017/9/14 创建了它
 */

public class GoodsActivity extends BaseActivity<ActivityGoodsBinding> {
    @Inject
    AppBar mAppBar;
    @Inject
    GoodsViewModel mViewModel;
    @Inject
    ArrayList<Fragment> mFragments;
    @Inject
    ArrayList<String> mTitles;
    @Inject
    FragmentPagerAdapter mPagerAdapter;

    private GoodsInfoFragment mInfoFragment;
    private GoodsPriceFragment mPriceFragment;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_goods;
    }

    @Override
    protected void initInjector() {
        DaggerGoodsActivityComponent.builder().appComponent(getAppComponent()).activityComponent(getActivityComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        mBinding.layoutDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mInfoFragment = GoodsInfoFragment.newsIntance();
        mPriceFragment = GoodsPriceFragment.newsIntance();
        String goodsId = getIntent().getStringExtra("goodsId");
        mAppBar.setTitle("商品编辑");
        mBinding.layoutAppbar.setAppbar(mAppBar);
        mBinding.setViewModel(mViewModel);
        mTitles.add("基本信息");
        mTitles.add("商品价格");
        mFragments.add(mInfoFragment);
        mFragments.add(mPriceFragment);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        NavBarUtils.setTabs(mBinding.magicIndicator, mTitles, mBinding.viewPager);
        setListener();
        mViewModel.getGoodsPropertys(goodsId);
    }

    private void setListener() {
        mViewModel.registerRxBus(GoodsEvent.class, goodsEvent -> {
            switch (goodsEvent.eventType) {
                case GoodsEvent.FLAG_PROPERTY_COMPLETE:
                    break;
                case GoodsEvent.FLAG_DETAIL_COMPLETE:
                    mInfoFragment.getViewModel().setGoodsDetails(mViewModel.getDetailResult(), mViewModel.getPropertyBean());
                    mPriceFragment.getViewModel().setPrice(mViewModel.getDetailResult());
                    break;
            }
        });

        mBinding.tvSave.setOnClickListener(v -> mViewModel.saveGoods(mInfoFragment.getViewModel(), mPriceFragment.getViewModel(), "8"));
        mBinding.tvSaveUpload.setOnClickListener(v -> mViewModel.saveGoods(mInfoFragment.getViewModel(), mPriceFragment.getViewModel(), "9"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        if (requestCode == 1000) {
            setProperty(data.getStringExtra("label"), data.getStringExtra("code"), data.getStringExtra("name"));
        } else
            mFragments.get(mBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }

    public void openGoodsPropertyFrag(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.layout_container, fragment).commit();
        mBinding.layoutDrawer.openDrawer(Gravity.RIGHT);
    }

    public void closeGoodsPropertyFrag() {
        mBinding.layoutDrawer.closeDrawer(Gravity.RIGHT);
    }

    public void setProperty(String label, String code, String name) {
        closeGoodsPropertyFrag();
        setProperty_no_close(label, code, name);
    }

    public void setProperty_no_close(String label, String code, String name) {
        Fragment fragment = mFragments.get(mBinding.viewPager.getCurrentItem());
        if (fragment instanceof GoodsInfoFragment) {
            ((GoodsInfoFragment) fragment).getViewModel().setLabelInfo(label, code, name);
        }
    }

    public GoodsViewModel getViewModel() {
        return mViewModel;
    }
}
